package br.com.guaraba.controlepatrimonio;

import br.com.guaraba.controlepatrimonio.model.Instituicao;
import br.com.guaraba.controlepatrimonio.model.dao.IDaoInstituicao;
import br.com.guaraba.controlepatrimonio.model.dao.impl.DaoInstituicaoImpl;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * Hello world!
 *
 */
public class App {

    static Logger logger = Logger.getLogger(App.class.getName());

    public static void main(String[] args) {
        PropertyConfigurator.configure("log4j.properties");

        System.out.println("Hello World! modificado no eclipse e no netbeans2");
        logger.info("Entering application.");

        if (logger.isDebugEnabled()) {
            logger.debug("Entry number: ");
        }
        
        
        Instituicao nova = new Instituicao(); //mobile.live.com
        nova.setNome("Agora com dao");
       // App.salvar(nova);
        EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("PatrimonioPU");
        IDaoInstituicao dao = new DaoInstituicaoImpl(emf);
        dao.create(nova);
        
        logger.info("Inst salva com sucesso");
    }

    public static void salvar(Instituicao instituicao) {
        try {
            EntityManagerFactory emf = javax.persistence.Persistence.createEntityManagerFactory("PatrimonioPU");
            EntityManager em = emf.createEntityManager();
            em.getTransaction().begin();
            em.persist(instituicao);
            em.getTransaction().commit();
        } finally {
            
        }
    }
}
